package com.heima.stroke.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    private static final long DELAY_TIME = 1000 * 30;

    public static final String STROKE_OVER_QUEUE = "STROKE_OVER_QUEUE";
    public static final String STROKE_DEAD_QUEUE = "STROKE_DEAD_QUEUE";
    public static final String STROKE_OVER_QUEUE_EXCHANGE = "STROKE_OVER_QUEUE_EXCHANGE";
    public static final String STROKE_DEAD_QUEUE_EXCHANGE = "STROKE_DEAD_QUEUE_EXCHANGE";
    public static final String STROKE_OVER_KEY = "STROKE_OVER_KEY";
    public static final String STROKE_DEAD_KEY = "STROKE_DEAD_KEY";

    @Bean
    public Queue strokeOverQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", STROKE_DEAD_QUEUE_EXCHANGE);
        args.put("x-dead-letter-routing-key", STROKE_DEAD_KEY);
        args.put("x-message-ttl", DELAY_TIME); // 设置消息过期时间
        return new Queue(STROKE_OVER_QUEUE, true, false, false, args);
    }

    @Bean
    public Queue strokeDeadQueue() {
        return new Queue(STROKE_DEAD_QUEUE, true, false, false);
    }

    @Bean
    DirectExchange strokeOverQueueExchange() {
        return new DirectExchange(STROKE_OVER_QUEUE_EXCHANGE);
    }

    @Bean
    DirectExchange strokeDeadQueueExchange() {
        return new DirectExchange(STROKE_DEAD_QUEUE_EXCHANGE);
    }

    @Bean
    Binding bindingStrokeOverDirect() {
        return BindingBuilder.bind(strokeOverQueue()).to(strokeOverQueueExchange()).with(STROKE_OVER_KEY);
    }

    @Bean
    Binding bindingStrokeDeadDirect() {
        return BindingBuilder.bind(strokeDeadQueue()).to(strokeDeadQueueExchange()).with(STROKE_DEAD_KEY);
    }
}